package com.unpay.other;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umpay.bean.Const;
import com.umpay.bean.MPermissions;
import com.umpay.bean.MerchantInfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginActivity extends Activity implements OnClickListener {
    
    public Button bt_login;
    
    public ArrayList<MPermissions> mPermissions;
    public String strJson;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.e("kk", "onCreate:" + System.currentTimeMillis());
        findView();
        init();
    }
    
    private void findView() {
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        Log.e("kk", "onNewIntent:" + System.currentTimeMillis());
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        Log.e("kk", "onResume:" + System.currentTimeMillis());
        super.onResume();
    }

    @Override
    protected void onRestart() {
        Log.e("kk", "onRestart:" + System.currentTimeMillis());
        super.onRestart();
    }

    @Override
    protected void onPause() {
        Log.e("kk", "onPause:" + System.currentTimeMillis());
        super.onPause();
    }
    
    @Override
    protected void onStop() {
        Log.e("kk", "onStop:" + System.currentTimeMillis());
        super.onStop();
    }

    private void init() {
        mPermissions = new ArrayList<MPermissions>();
        for (int i = 0; i < 6; i++) {
            MPermissions permissions = new MPermissions();
            if (i == 2) {
                permissions.pkg = "com.ncf.fangdaip2p";
                permissions.status = "0";// 0 未开通授权 1 已开通授权
                permissions.isEmpty = 0;
            } else if (i == 3) {
                permissions.status = "1";
                permissions.pkg = "com.umpay.huishangjia.csm";
            } else {
                permissions.status = "1";
                permissions.isEmpty = 0;
                permissions.pkg = "com.ncf.yrjfp2p";
            }
            permissions.isEmpty = 0;
            permissions.name = "微信";
            permissions.version = "8.1.0";
            permissions.appUrl = "http://www.baidu.com";
            
            String page_location;
            int cell_location;
            if (i / 5 == 0) {
                page_location = "0";
            } else {
                page_location = "1";
            }
            if (i >= 5) {
                cell_location = i - 5;
            } else {
                cell_location = i;
            }
            permissions.page_location = page_location;
            permissions.cell_location = cell_location + "";
            if (i == 2) {
                permissions.page_location = page_location;
                permissions.cell_location = cell_location + "";
            }
            mPermissions.add(permissions);
        }
        getLoginInfo();
        
    }
    
    @Override
    public void onClick(View v) {
        
        switch (v.getId()) {
            case R.id.bt_login:

                // Toast.makeText(LoginActivity.this, "登录成功", 0).show();
                
                sendToLauncher();
                LoginActivity.this.finish();
                break;
                
                
        }
    }
    
    /**
     * 配置json 信息
     *
     * @return
     */
    private String getLoginInfo() {
        
        JSONObject jsonObject = new JSONObject();
        
        try {
            JSONObject jsonObject2 = getInfo();
            JSONArray array = getAppArray();
            jsonObject.put("merchant_info", jsonObject2);
            jsonObject.put("merchant_permissions", array);
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
        strJson = jsonObject.toString();
        return strJson;
        
    }
    
    private JSONArray getAppArray() {
        JSONArray array = new JSONArray();
        
        for (int i = 0; i < mPermissions.size(); i++) {
            MPermissions pp = mPermissions.get(i);
            JSONObject child = new JSONObject();
            try {
                child.put("pkg", pp.pkg);
                child.put("name", pp.name);
                child.put("status", pp.status);
                child.put("isEmpty", pp.isEmpty);
                child.put("page_location", pp.page_location);
                child.put("cell_location", pp.cell_location);
                child.put("version", pp.version);
                child.put("appUrl", pp.appUrl);
                array.put(child);
                
                
            } catch (JSONException e) {
                e.printStackTrace();
            }
            
        }
        return array;
    }
    
    
    private JSONObject getInfo() throws JSONException {
        
        JSONObject jsonObject = new JSONObject();
        MerchantInfo info = new MerchantInfo();
        info.id = "000000001";
        info.name = "无敌KFC";
        jsonObject.put("id", info.id);
        jsonObject.put("name", info.name);
        
        return jsonObject;
    }
    
    /**
     * 发送消息给
     */
    private void sendToLauncher() {
        
        Intent intent = new Intent();
        intent.setAction(Const.ACTION_LAUNCHER);
        intent.setPackage(Const.PACKAGE_LAUNCHER);
        intent.putExtra("type", Const.TYPE_LOGIN);
        intent.putExtra("status", 0);
        intent.putExtra("des", "登录成功");
        intent.putExtra("data", strJson);
        startService(intent);
        
        
    }
    
    
}
